import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Layout from '@/layout/index.vue'
// 由于懒加载页面太多的话会造成webpack热更新太慢，所以开发环境不使用懒加载，只有生产环境使用懒加载
const _import = require('@/libs/util.import.' + process.env.NODE_ENV)
Vue.use(VueRouter)

export const constantRoutes: RouteConfig[] = [
  {
    path: '/login',
    component: _import('system/login'),
    meta: {
      title: '登录',
      hidden: true
    }
  },
  {
    path: '/404',
    component: _import('error-page/404.vue'),
    meta: {
      hidden: true
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: _import('system/dashboard'),
        name: 'Dashboard',
        meta: {
          title: '首页',
          icon: 'dashboard',
          affix: true
        }
      }
    ]
  },
  {
    path: '/guide',
    component: Layout,
    redirect: '/guide/index',
    children: [
      {
        path: 'index',
        component: _import('system/guide'),
        name: 'Guide',
        meta: {
          title: '指导栏',
          icon: 'guide',
          noCache: true
        }
      }
    ]
  },
  {
    path: '/table',
    component: Layout,
    redirect: '/table/list',
    meta: {
      title: 'table管理',
      icon: 'table'
    },
    children: [
      {
        path: 'list',
        component: _import('system/table'),
        name: 'table-list',
        meta: {
          title: '普通table'
          // icon: "list" 这是子菜单的icon
        }
      },
      {
        path: 'checkboxTable',
        component: _import('system/checkboxTable'),
        name: 'checkbox-table',
        meta: {
          title: '改变表头table'
          // icon: "list"
        }
      },
      {
        path: 'dragTable',
        component: _import('system/table/drag-table'),
        name: 'drag-table',
        meta: {
          title: '拖拽table'
        }
      }
    ]
  },
  {
    path: '/form',
    component: Layout,
    redirect: '/form/index',
    meta: {
      title: 'form管理',
      icon: 'form'
    },
    children: [
      {
        path: 'index',
        component: _import('system/formModule'),
        name: 'form',
        meta: {
          title: 'public form'
        }
      },
      {
        path: 'dragForm',
        component: _import('system/formModule/drag-form'),
        name: 'drag-form',
        meta: {
          title: '拖拽form'
        }
      }
    ]
  },
  {
    path: '/editImg',
    component: Layout,
    redirect: '/editImage/rotateImg',
    meta: {
      title: '图片编辑',
      icon: 'edit'
    },
    children: [
      {
        path: 'rotateImg',
        component: _import('system/editImage/rotateImg'),
        name: 'rotateImg',
        meta: {
          title: '旋转生成图片'
        }
      },
      {
        path: 'viewerImg',
        component: _import('system/editImage/viewerImg'),
        name: 'viewerImg',
        meta: {
          title: '插件操作图片'
        }
      },
      {
        path: 'watermark-picture',
        component: _import('system/editImage/watermarkPicture'),
        name: 'watermarkPicture',
        meta: {
          title: '水印图片'
        }
      }
    ]
  }
]

export const asyncRoutes: RouteConfig[] = [
  {
    path: '/system',
    component: Layout,
    redirect: '/system/menu',
    meta: {
      title: '系统管理',
      icon: 'guide',
      roles: ['admin'],
      alwaysShow: true
    },
    children: [
      {
        path: 'menu',
        component: _import('admin/menu'),
        name: 'system-menu',
        meta: {
          title: 'menu',
          // icon: 'menu',
          roles: ['admin']
        }
      },
      {
        path: 'role',
        component: _import('admin/role'),
        name: 'system-role',
        meta: {
          title: 'role',
          // icon: 'user',
          roles: ['admin']
        }
      },
      {
        path: 'user',
        component: _import('admin/user'),
        name: 'system-user',
        meta: {
          title: 'user',
          // icon: 'user',
          roles: ['admin']
        }
      }
    ]
  },
  {
    path: '*',
    redirect: '/404',
    meta: { hidden: true }
  }
]

const createRouter = () =>
  new VueRouter({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes: constantRoutes
  })
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher // reset router
}
export default router
